<?php
/**
 * 链动会员模型关联测试文件
 */

// 测试基本的模型关联查询
use addon\lian\model\sqlmodel\LianMemberModel;

// 测试1: 基本查询
function testBasicQuery() {
    try {
        $model = new LianMemberModel();
        
        // 测试基本的关联查询
        $result = $model->with(['member', 'level'])
            ->where('site_id', 1)
            ->limit(1)
            ->select();
            
        echo "基本查询测试:\n";
        if ($result->isEmpty()) {
            echo "- 没有找到数据\n";
        } else {
            echo "- 查询成功，找到 " . $result->count() . " 条记录\n";
            $first = $result->first();
            echo "- 第一条记录ID: " . $first->id . "\n";
            if ($first->member) {
                echo "- 会员昵称: " . $first->member->nickname . "\n";
            }
            if ($first->level) {
                echo "- 等级名称: " . $first->level->level_name . "\n";
            }
        }
        
        return true;
    } catch (Exception $e) {
        echo "基本查询测试失败: " . $e->getMessage() . "\n";
        return false;
    }
}

// 测试2: 分页查询
function testPaginationQuery() {
    try {
        $model = new LianMemberModel();
        
        $condition = [
            ['site_id', '=', 1]
        ];
        
        $result = $model->getLianMemberPageListWithRelation($condition, 1, 5);
        
        echo "\n分页查询测试:\n";
        if ($result['code'] == 0) {
            echo "- 查询成功\n";
            echo "- 总记录数: " . $result['data']['count'] . "\n";
            echo "- 当前页记录数: " . count($result['data']['list']) . "\n";
            
            if (!empty($result['data']['list'])) {
                $first = $result['data']['list'][0];
                echo "- 第一条记录:\n";
                echo "  ID: " . $first['id'] . "\n";
                echo "  会员ID: " . $first['member_id'] . "\n";
                echo "  昵称: " . ($first['nickname'] ?? '未知') . "\n";
                echo "  等级: " . ($first['level_name'] ?? '未设置') . "\n";
            }
        } else {
            echo "- 查询失败: " . $result['message'] . "\n";
        }
        
        return $result['code'] == 0;
    } catch (Exception $e) {
        echo "分页查询测试失败: " . $e->getMessage() . "\n";
        return false;
    }
}

// 测试3: 搜索查询
function testSearchQuery() {
    try {
        $model = new LianMemberModel();
        
        $condition = [
            ['site_id', '=', 1]
        ];
        
        // 测试昵称搜索
        $result = $model->getLianMemberPageListWithRelation($condition, 1, 5, 'create_time desc', '测试', '');
        
        echo "\n搜索查询测试:\n";
        if ($result['code'] == 0) {
            echo "- 昵称搜索成功\n";
            echo "- 找到记录数: " . $result['data']['count'] . "\n";
        } else {
            echo "- 昵称搜索失败: " . $result['message'] . "\n";
        }
        
        return $result['code'] == 0;
    } catch (Exception $e) {
        echo "搜索查询测试失败: " . $e->getMessage() . "\n";
        return false;
    }
}

// 运行测试
echo "=== 链动会员模型关联测试 ===\n";

$test1 = testBasicQuery();
$test2 = testPaginationQuery();
$test3 = testSearchQuery();

echo "\n=== 测试结果 ===\n";
echo "基本查询: " . ($test1 ? "通过" : "失败") . "\n";
echo "分页查询: " . ($test2 ? "通过" : "失败") . "\n";
echo "搜索查询: " . ($test3 ? "通过" : "失败") . "\n";

if ($test1 && $test2) {
    echo "\n✅ 主要功能测试通过，模型关联工作正常！\n";
} else {
    echo "\n❌ 测试失败，请检查数据库连接和表结构\n";
}

// 使用说明
echo "\n=== 使用说明 ===\n";
echo "1. 确保数据库中有 lian_member、member、lian_level 表\n";
echo "2. 确保表中有测试数据\n";
echo "3. 在控制器中使用 use_relation=1 参数启用关联查询\n";
echo "4. 关联查询不使用表别名，直接使用字段名\n";
echo "5. 搜索功能通过 whereHas 实现关联表查询\n";
